package com.dtx.module.quickstart;

import com.dtx.entity.SysUser;
import com.zaxxer.hikari.HikariDataSource;
import org.beetl.sql.core.*;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.ext.DebugInterceptor;

import javax.sql.DataSource;
import java.util.List;
import java.util.Set;
public class QuickTest {

    private static  DataSource datasource() {
        HikariDataSource ds = new HikariDataSource();
        //内存数据库
        ds.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/beetlsql?characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC");
        ds.setUsername("root");
        ds.setPassword("123456");
        ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
        return ds;
    }

    private  static SQLManager getSQLManager(){
        //得到一个数据源
        DataSource dataSource = datasource();
        //得到一个ConnectionSource， 单数据源
        ConnectionSource source = ConnectionSourceHelper.getSingle(dataSource);
        //SQLManagerBuilder 唯一必须的参数就是ConnectionSource
        SQLManagerBuilder builder = new SQLManagerBuilder(source);
        //命名转化，数据库表和列名下划线风格，转化成Java对应的首字母大写，比如create_time 对应ceateTime
        builder.setNc(new UnderlinedNameConversion());
        //拦截器，非必须，这里设置一个debug拦截器，可以详细查看执行后的sql和sql参数
        builder.setInters(new Interceptor[]{new DebugInterceptor()});
        //数据库风格，因为用的是H2,所以使用H2Style,
        builder.setDbStyle(new MySqlStyle());
        SQLManager sqlManager = builder.build();
        return sqlManager;
    }

    public static void main(String[] args) throws Exception {
        SQLManager sqlManager = getSQLManager();
        //初始化数据脚本，执行后，内存数据库将有一个sys_user表和模拟数据
        //DBInitHelper.executeSqlScript(sqlManager,"db/schema.sql");
        // 得到数据库的所有表
        Set<String> all =  sqlManager.getMetaDataManager().allTable();
        System.out.println(all);
        System.out.println("=============");
        SysUser user  = sqlManager.unique(SysUser.class,1);
        System.out.println(user);
        String sql = "select * from sys_user where id=?";
        Integer id  = 1;
        SQLReady sqlReady = new SQLReady(sql,new Object[]{id});
        List<SysUser> userEntities = sqlManager.execute(sqlReady, SysUser.class);
        System.out.println(userEntities);

    }

}
